package com.example.arouter

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.tencent.mmkv.MMKV
/**
 * 路由参数拦截器
 * */

@Interceptor(priority = 4, name = "路由参数拦截器")
class RouterParamInterceptor :IInterceptor{

    private lateinit var mContext : Context
    //白名单集合
    private val checkPath = arrayOf("/activity/main","/activity/detail")

    override fun init(context: Context?) {
        if(context!=null){
            mContext=context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        try {
            val routerPath= postcard.path
            val sessionId= MMKV.defaultMMKV().decodeString("sessionId","123")
            //获取router携带的参数
            val params = postcard.extras
            //检查是否存在白名单中
            if (checkPath.contains(routerPath) && !params.containsKey("sessionId")){
                postcard.withString("sessionId",sessionId)
            }
            //最终要释放页面的跳转逻辑继续执行
            callback.onContinue(postcard)
        } catch (e: Exception) {
            e.printStackTrace()
            callback.onContinue(postcard)
        }

    }

}